/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mx.edu.itschapala.sistemas.septimo.vista;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import mx.edu.itschapala.sistemas.septimo.control.ClienteLnLocal;
import mx.edu.itschapala.sistemas.septimo.modelo.Cliente;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TiposBusqueda;

/**
 *
 * @author ZATHLER
 */
public class ClienteListar extends javax.swing.JFrame {

    private ClienteLnLocal clienteControl;
    private DefaultTableModel modelo;
    private List<Cliente> lista;
    private JFrame d;
    /**
     * Creates new form ClienteListar
     */
    public ClienteListar(ClienteLnLocal clienteControl) {
        this.clienteControl=clienteControl;
        initComponents();
        String nombres[] = {"Id", "Nombre", "Telefono", "Direccion", "Sexo"};
                modelo = new DefaultTableModel(nombres, 0) {
                    public boolean isCellEditable(int row, int column) {
                        return false;
                    }
                };
                jTable1.setModel(modelo);
                lista = clienteControl.getTodos();
                for (Cliente p : lista) {
                    String renglon[] = new String[6];
                    renglon[0] = p.getId() + "";
                    renglon[1] = p.getNombre();
                    renglon[2] = p.getTelefono() + "";
                    renglon[3] = p.getDireccion();
                    renglon[4] = p.getSexo()+"";
                    modelo.addRow(renglon);
                }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        boGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        textBus = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        boGroup1.add(jRadioButton1);
        jRadioButton1.setText("Contiene");

        boGroup1.add(jRadioButton2);
        jRadioButton2.setText("Inicia");

        textBus.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                textBusKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioButton1)
                                .addGap(68, 68, 68)
                                .addComponent(textBus, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jRadioButton1)
                    .addComponent(textBus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void textBusKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textBusKeyPressed
        List<Cliente> tmp = new ArrayList<Cliente>();
        if (jRadioButton1.isSelected()) {
            tmp = clienteControl.getProNombre(textBus.getText() + evt.getKeyChar(), TiposBusqueda.CONTIENE);
        } else {
            tmp = clienteControl.getProNombre(textBus.getText() + evt.getKeyChar(), TiposBusqueda.INICIA);
        }
        String nombres[] = {"Id", "Nombre", "Telefono", "Direccion", "Sexo"};
        modelo = new DefaultTableModel(nombres, 0) {
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        for (Cliente p : tmp) {
            String renglon[] = new String[5];
            renglon[0] = p.getId() + "";
            renglon[1] = p.getNombre();
            renglon[2] = p.getTelefono() + "";
            renglon[3] = p.getDireccion();
            renglon[4] = p.getSexo()+"";
            modelo.addRow(renglon);
        }
        jTable1.setModel(modelo);
        lista = tmp;
    }//GEN-LAST:event_textBusKeyPressed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
         if (evt.getClickCount() == 2) {
            Cliente p = lista.get(jTable1.getSelectedRow());
            if (d == null) {
                d = new DialogoC(p, clienteControl);

            } else {
                d.dispose();
                d = new DialogoC(p, clienteControl);
            }
            d.setVisible(true);
        }
    }//GEN-LAST:event_jTable1MouseClicked

  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup boGroup1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField textBus;
    // End of variables declaration//GEN-END:variables
}
